<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="100px"
    >
      <el-form-item label="光开关" prop="switchId">
        <el-input
          :disabled="true"
          v-model="dataForm.switchId"
          placeholder="1-64"
        ></el-input>
      </el-form-item>
      <el-form-item label="监测点" prop="measurePoint">
        <el-input
          v-model="dataForm.measurePoint"
          placeholder="监测点"
        ></el-input>
      </el-form-item>
      <el-form-item label="FBG的数量" prop="fbgNum">
        <el-input
          v-model="dataForm.fbgNum"
          placeholder="0-20(0表示禁用 20表示基准)"
        ></el-input>
      </el-form-item>
      <el-form-item label="段id" prop="partId">
        <el-input
          v-model="dataForm.partId"
          placeholder="0-16 0表示禁用"
        ></el-input>
      </el-form-item>
      <el-form-item label="方向" prop="face">
        <el-select v-model="dataForm.face" clearable placeholder="方向">
          <el-option :value="0" label="正面" />
          <el-option :value="1" label="反面" />
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        switchId: "",
        measurePoint: "",
        fbgNum: "",
        partId: "",
        face: "",
        gmtCreate: "",
        gmtModify: ""
      },
      dataRule: {
        switchId: [
          {
            required: true,
            message: "光开关ID（1-64）不能为空",
            trigger: "blur"
          }
        ],
        fbgNum: [
          {
            required: true,
            message: "每个光开关对应的fbg的数量不能为空",
            trigger: "blur"
          }
        ],
        partId: [
          {
            required: true,
            message: "段id的不能为空",
            trigger: "blur"
          }
        ],
        face: [
          {
            required: true,
            message: "方向不能为空",
            trigger: "blur"
          }
        ],
        gmtCreate: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        gmtModify: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/generator/switchtofbgconfig/info/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.switchId = data.switchToFbgConfig.switchId;
              this.dataForm.measurePoint = data.switchToFbgConfig.measurePoint;
              this.dataForm.fbgNum = data.switchToFbgConfig.fbgNum;
              this.dataForm.partId = data.switchToFbgConfig.partId;
              this.dataForm.face = data.switchToFbgConfig.face;
              this.dataForm.gmtCreate = data.switchToFbgConfig.gmtCreate;
              this.dataForm.gmtModify = data.switchToFbgConfig.gmtModify;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/generator/switchtofbgconfig/${
                !this.dataForm.id ? "save" : "update"
              }`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              switchId: this.dataForm.switchId,
              measurePoint: this.dataForm.measurePoint,
              fbgNum: this.dataForm.fbgNum,
              partId: this.dataForm.partId,
              face: this.dataForm.face,
              gmtCreate: this.dataForm.gmtCreate,
              gmtModify: this.dataForm.gmtModify
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "修改成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    }
  }
};
</script>
